//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zaicheng.cloud.remotecall.template;

import com.zaicheng.cloud.remotecall.Util;

import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public final class HeaderTemplate extends Template {
    private LinkedHashSet<String> values;
    private String name;

    public static HeaderTemplate from(String name, List<TemplateChunk> chunks) {
        if (name != null && !name.isEmpty()) {
            if (chunks == null) {
                throw new IllegalArgumentException("chunks are required.");
            } else {
                return new HeaderTemplate(name, Util.UTF_8, chunks);
            }
        } else {
            throw new IllegalArgumentException("name is required.");
        }
    }

    public static HeaderTemplate create(String name, Iterable<String> values) {
        if (name != null && !name.isEmpty()) {
            if (values == null) {
                throw new IllegalArgumentException("values are required");
            } else {
                StringBuilder template = new StringBuilder();
                template.append(name).append(" ");
                Iterator iterator = values.iterator();

                while(iterator.hasNext()) {
                    template.append((String)iterator.next());
                    if (iterator.hasNext()) {
                        template.append(",");
                    }
                }

                return new HeaderTemplate(template.toString(), name, values, Util.UTF_8);
            }
        } else {
            throw new IllegalArgumentException("name is required.");
        }
    }

    public static HeaderTemplate append(HeaderTemplate headerTemplate, Iterable<String> values) {
        LinkedHashSet<String> headerValues = new LinkedHashSet(headerTemplate.getValues());
        headerValues.addAll((Collection)StreamSupport.stream(values.spliterator(), false).filter(Util::isNotBlank).collect(Collectors.toCollection(LinkedHashSet::new)));
        return create(headerTemplate.getName(), headerValues);
    }

    public static HeaderTemplate appendFrom(HeaderTemplate headerTemplate, List<TemplateChunk> chunks) {
        List<TemplateChunk> existing = new CopyOnWriteArrayList(headerTemplate.getTemplateChunks());
        existing.addAll(chunks);
        return from(headerTemplate.getName(), existing);
    }

    private HeaderTemplate(String template, String name, Iterable<String> values, Charset charset) {
        super(template, ExpansionOptions.REQUIRED, EncodingOptions.NOT_REQUIRED, false, charset);
        this.values = (LinkedHashSet)StreamSupport.stream(values.spliterator(), false).filter(Util::isNotBlank).collect(Collectors.toCollection(LinkedHashSet::new));
        this.name = name;
    }

    private HeaderTemplate(String name, Charset charset, List<TemplateChunk> chunks) {
        super(ExpansionOptions.REQUIRED, EncodingOptions.NOT_REQUIRED, false, charset, chunks);
        this.values = (LinkedHashSet)chunks.stream().map(TemplateChunk::getValue).collect(Collectors.toCollection(LinkedHashSet::new));
        this.name = name;
    }

    public Collection<String> getValues() {
        return Collections.unmodifiableCollection(this.values);
    }

    public String getName() {
        return this.name;
    }

    public String expand(Map<String, ?> variables) {
        String result;
        for(result = super.expand(variables); result.endsWith(","); result = result.replaceAll(",$", "")) {
        }

        result = result.replaceAll(",", ", ");
        return result;
    }
}
